模块:SMW query
local getArgs = require('Module:Arguments').getArgs local util = require('Module:Util') local xtable = require('Module:Table') local p = {} local shared = {} local data = {} local g_args, g_frame -- ---------------------------------------------------------------------------- -- Section shared functions -- ---------------------------------------------------------------------------- shared.mod = {} function shared.mod.header(row) local stat = mw.html.create('span') local text, nsub = mw.ustring.gsub(rowstat text', '%d+', '?') stat :attr('class', 'mod-table-header-stat') :wikitext(text) :done() local mgroup = mw.html.create('span') mgroup :attr('class', 'mod-table-header-modgroup') :wikitext(rowmod group') :done() local tbl = mw.html.create('table') tbl :attr('class', 'wikitable mw-collapsible mw-collapsed mod-table') :tag('tr') :tag('th') :attr('class', 'mod-table-header') :attr('colspan', g_args.colspan) :tag('span') :attr('class', 'mod-table-header-container') :wikitext(tostring(stat) .. tostring(mgroup)) :done() :done() return tbl end function shared.mod.format(tbl, row, tags) local tr = tbl:tag('tr') tr :tag('td') :wikitext(string.format('%s', row1, rowname')) :attr('class', 'mod-table-cell-name') :done() :tag('td') :wikitext(rowlevel requirement') :attr('class', 'mod-table-cell-level') :done() :tag('td') :wikitext(rowstat text') :attr('class', 'mod-table-cell-stat') :done() :tag('td') :wikitext(table.concat(tags, ', ')) :attr('class', 'mod-table-cell-tags') :done() end -- ---------------------------------------------------------------------------- -- Section specific templates -- ---------------------------------------------------------------------------- -- -- Template: SMW query mods -- function p.query_mods(frame) -- Args g_args = getArgs(frame, { parentFirst = true }) g_frame = util.misc.get_frame(frame) g_args.colspan = 4 local conditions = {} conditions#conditions+1 = 'concept' if g_args.tag then conditions#conditions+1 = string.format('Has subobject::[[-Has subobject::+ Has spawn weight::>>0 Has tag::%s]]', g_args.tag) end g_args.header_level = g_args.header_level or 2 -- Fields local fields = {} fields#fields+1 = '?Is mod' fields#fields+1 = '?Has name' fields#fields+1 = '?Has level requirement' fields#fields+1 = '?Has mod group' fields#fields+1 = '?Has stat text' -- parameters local parameters = {} parameters.sort = 'Has mod group, ' parameters.limit = 1000 -- lets see local data = {} data.header = { prefix = 'Prefix', suffix = 'Suffix', } local out = {} for _, v in ipairs({'prefix', 'suffix'}) do out#out+1 = string.format('%s', g_args.header_level, data.headerv, g_args.header_level) conditions1 = string.format('Concept:Spawnable named %s item mods', v) local query local results -- -- Query tags -- query = {} query#query+1 = string.format('-Has subobject::%s', table.concat(conditions, ' ')) query#query+1 = 'Has tag::+' query#query+1 = 'Has spawn weight::+' --query#query+1 = 'Has spawn weight::>>0' query#query+1 = '?Has tag' query#query+1 = '?Has spawn weight#' -- need native number query.limit = 1000 query.offset = 0 -- Tag order is very important query.sort = ', Is tag number' local tags = {} -- this works because lua only considers nil to be false >_> while query.offset do results = util.smw.query(query, g_frame) query.offset = query.offset + #results -- terminates the while if enough reuslts have been fetched if query.offset % 1000 ~= 0 then query.offset = nil end for _, row in ipairs(results) do local page, _ = string.gsub(row1, '#_%x+', '') if tagspage nil then tagspage = {} end local text if tonumber(rowspawn weight') > 0 then text = ' ' else text = ' ' end tagspage[#tagspage+1] = string.format('%s %s', rowtag', text) end end -- -- Query mods -- query = {} for _, v in ipairs(conditions) do query#query+1 = v end for _, v in ipairs(fields) do query#query+1 = v end for k, v in pairs(parameters) do queryk = v end results = util.smw.query(query, g_frame) local last = '' local tbl = '' for _, row in ipairs(results) do local current = string.gsub(rowmod', '%d+.*', '%%d.*') if string.match(last, current) then shared.mod.format(tbl, row, tags[row1]) else out#out+1 = tostring(tbl) tbl = shared.mod.header(row) shared.mod.format(tbl, row, tags[row1]) end last = rowmod' end -- add the last table out#out+1 = tostring(tbl) end return table.concat(out, '') end function p.test(frame) -- Args g_args = getArgs(frame, { parentFirst = true }) g_frame = util.misc.get_frame(frame) local tquery = {} -- selectors tquery#tquery+1 = '-Has subobject::[[Concept:Spawnable named prefix mods Has mod domain::1]]' tquery#tquery+1 = 'Has tag::+' tquery#tquery+1 = 'Has spawn weight::>>0' tquery#tquery+1 = '?Has tag' -- extra tquery.limit = 5000 -- server-side limit, hope it's enough local c = os.clock() local results = util.smw.query(tquery, g_frame) tags = {} for _, row in ipairs(results) do local page, _ = string.gsub(row1, '#_%x+', '') if tagspage nil then tagspage = {} end table.insert(tagspage, rowtag') end mw.log('Total ' .. (os.clock() - c)) mw.logObject(#results) end return p